package com.codeczx.customviewdemo.customweekview;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.codeczx.customviewdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by codeczx on 2017/9/28.
 */

public class CustomWeekView extends LinearLayout {
    private static final String TAG = "CustomWeekView";
    @BindView(R.id.tv_week6)
    TextView tvWeek6;
    @BindView(R.id.ll_item6)
    LinearLayout llItem6;
    @BindView(R.id.tv_week7)
    TextView tvWeek7;
    @BindView(R.id.ll_item7)
    LinearLayout llItem7;
    @BindView(R.id.tv_week1)
    TextView tvWeek1;
    @BindView(R.id.ll_item1)
    LinearLayout llItem1;
    @BindView(R.id.tv_week2)
    TextView tvWeek2;
    @BindView(R.id.ll_item2)
    LinearLayout llItem2;
    @BindView(R.id.tv_week3)
    TextView tvWeek3;
    @BindView(R.id.ll_item3)
    LinearLayout llItem3;
    @BindView(R.id.tv_week4)
    TextView tvWeek4;
    @BindView(R.id.ll_item4)
    LinearLayout llItem4;
    @BindView(R.id.tv_week5)
    TextView tvWeek5;
    @BindView(R.id.ll_item5)
    LinearLayout llItem5;
    @BindView(R.id.tv_week8)
    TextView tvWeek8;
    @BindView(R.id.ll_item8)
    LinearLayout llItem8;
    @BindView(R.id.tv_week9)
    TextView tvWeek9;
    @BindView(R.id.ll_item9)
    LinearLayout llItem9;
    private Context mContext;
    private int itemWidth;

    private String[] str1 = {"6", "7", "1", "2", "3", "4", "5", "6", "7"};
    private String[] str2 = {"7", "1", "2", "3", "4", "5", "6", "7", "1"};
    private String[] str3 = {"1", "2", "3", "4", "5", "6", "7", "1", "2"};
    private String[] str4 = {"2", "3", "4", "5", "6", "7", "1", "2", "3"};
    private String[] str5 = {"3", "4", "5", "6", "7", "1", "2", "3", "4"};
    private String[] str6 = {"4", "5", "6", "7", "1", "2", "3", "4", "5"};
    private String[] str7 = {"5", "6", "7", "1", "2", "3", "4", "5", "6"};
    private List<String[]> strList;

    public CustomWeekView(Context context) {
        this(context, null);
    }

    public CustomWeekView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomWeekView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.custom_week_view, this, true);
        ButterKnife.bind(this);
        strList = new ArrayList<>();
        strList.add(str1);
        strList.add(str2);
        strList.add(str3);
        strList.add(str4);
        strList.add(str5);
        strList.add(str6);
        strList.add(str7);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureInit();

    }

    private void measureInit() {
        ViewGroup container = ((ViewGroup) getChildAt(0));
        int childCount = container.getChildCount();
        itemWidth = getMeasuredWidth() / 5;
        ViewGroup child;
        Log.i(TAG, "measureInit: " + getMeasuredWidth());
        Log.i(TAG, "measureInit: " + childCount);
        for (int i = 0; i < childCount; i++) {
            child = (ViewGroup) container.getChildAt(i);
            LinearLayout.LayoutParams params = (LayoutParams) child.getLayoutParams();
            params.width = itemWidth;
            Log.i(TAG, "measureInit: " + itemWidth);
            child.setLayoutParams(params);
        }
    }

    @OnClick({R.id.ll_item6, R.id.ll_item7, R.id.ll_item1, R.id.ll_item2, R.id.ll_item3, R.id.ll_item4, R.id.ll_item5, R.id.ll_item8, R.id.ll_item9})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_item6:
                break;
            case R.id.ll_item7:
                break;
            case R.id.ll_item1:
                startAnimation(1, view);
                break;
            case R.id.ll_item2:
                startAnimation(2, view);
                break;
            case R.id.ll_item3:
                startAnimation(3, view);
                break;
            case R.id.ll_item4:
                startAnimation(4, view);
                break;
            case R.id.ll_item5:
                startAnimation(5, view);
                break;
            case R.id.ll_item8:
                break;
            case R.id.ll_item9:
                break;
        }
    }

    private List<LinearLayout> llList = new ArrayList<>();

    private void startAnimation(final int selectedItem, View llClicked) {
        Log.i(TAG, "startAnimation: ");

        ViewGroup container = ((ViewGroup) getChildAt(0));
        int childCount = container.getChildCount();
        for (int i = 0; i < childCount; i++) {
            llList.add((LinearLayout) container.getChildAt(i));
        }
        int offsetUnit = llItem3.getWidth();
//        ObjectAnimator animator1 = ObjectAnimator.ofFloat(llItem1,"X",llItem1.getX(),llItem1.getX()+offset);
//        ObjectAnimator animator2 = ObjectAnimator.ofFloat(llItem2,"X",llItem2.getX(),llItem2.getX()+offset);
//        ObjectAnimator animator3 = ObjectAnimator.ofFloat(llItem3,"X",llItem3.getX(),llItem3.getX()+offset);
//        ObjectAnimator animator4 = ObjectAnimator.ofFloat(llItem4,"X",llItem4.getX(),llItem4.getX()+offset);
//        ObjectAnimator animator5 = ObjectAnimator.ofFloat(llItem5,"X",llItem5.getX(),llItem5.getX()+offset);
//        ObjectAnimator animator100 = ObjectAnimator.ofFloat(llItem2,"scaleX",1.0f,1.5f);
//        ObjectAnimator animator101 = ObjectAnimator.ofFloat(llItem2,"scaleY",1.0f,1.5f);
//        AnimatorSet set = new AnimatorSet();
//        set.playTogether(animator1,animator2,animator3,animator4,animator5,animator100,animator101);
//        set.start();

        final int offsetCount = 3 - selectedItem;
        int offset = offsetUnit * offsetCount;
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(llItem1, "X", llItem1.getX(), llItem1.getX() + offset);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(llItem2, "X", llItem2.getX(), llItem2.getX() + offset);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(llItem3, "X", llItem3.getX(), llItem3.getX() + offset);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(llItem4, "X", llItem4.getX(), llItem4.getX() + offset);
        ObjectAnimator animator5 = ObjectAnimator.ofFloat(llItem5, "X", llItem5.getX(), llItem5.getX() + offset);
        ObjectAnimator animator6 = ObjectAnimator.ofFloat(llItem6, "X", llItem6.getX(), llItem6.getX() + offset);
        ObjectAnimator animator7 = ObjectAnimator.ofFloat(llItem7, "X", llItem7.getX(), llItem7.getX() + offset);
        ObjectAnimator animator8 = ObjectAnimator.ofFloat(llItem8, "X", llItem8.getX(), llItem8.getX() + offset);
        ObjectAnimator animator9 = ObjectAnimator.ofFloat(llItem9, "X", llItem9.getX(), llItem9.getX() + offset);

        ObjectAnimator animator100 = ObjectAnimator.ofFloat(llItem2, "scaleX", 1.0f, 1.5f);
        ObjectAnimator animator101 = ObjectAnimator.ofFloat(llItem2, "scaleY", 1.0f, 1.5f);
        ObjectAnimator animator102 = ObjectAnimator.ofFloat(llItem2, "scaleX", 1.5f, 1.0f);
        ObjectAnimator animator103 = ObjectAnimator.ofFloat(llItem2, "scaleY", 1.5f, 1.0f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator1, animator2, animator3, animator4, animator5, animator6,
                animator7, animator8, animator9, animator100, animator101, animator102, animator103);
        set.start();
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                setCenter(selectedItem);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    private void setCenter(int selectedItem) {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 9; j++) {
                if (llList.get(j).getX() == itemWidth * i) {
                    LinearLayout ll = llList.get(j);
//                    llList.remove(j);
                    switch (i) {
                        case 0:
                            llItem1 = ll;
                            break;
                        case 1:
                            llItem2 = ll;
                            break;
                        case 2:
                            llItem3 = ll;
                            break;
                        case 3:
                            llItem4 = ll;
                            break;
                        case 4:
                            llItem5 = ll;
                            break;
                    }
                }
            }
        }
        for (int i = 0; i < llList.size(); i++) {
            LinearLayout ll = llList.get(i);
            switch (i) {
                case 0:
                    ll.setX(-itemWidth * 2);
                    llItem6 = ll;
                    break;
                case 1:
                    ll.setX(-itemWidth);
                    llItem7 = ll;
                    break;
                case 2:
                    ll.setX(itemWidth);
                    llItem8 = ll;
                    break;
                case 3:
                    ll.setX(itemWidth * 2);
                    llItem9 = ll;
                    break;
            }
        }
        llList.clear();
        reboundDataByCenter(selectedItem);
    }

    private void reboundDataByCenter(int selectedItem) {
        ViewGroup container = ((ViewGroup) getChildAt(0));
        int childCount = container.getChildCount();
        String[] temp = strList.get(selectedItem);
        for (int i = 0; i < childCount; i++) {
            llList.add((LinearLayout) container.getChildAt(i));
        }
        for (int i = 0; i < llList.size(); i++) {
            TextView tv = (TextView) llList.get(i).getChildAt(0);
            tv.setText(temp[i]);
        }
    }
}
